<?php
class MY_Controller extends CI_Controller{
	
	function __construct(){
		parent::__construct();
//		$this->output->enable_profiler(TRUE);
	}
	
	/*
	 * $data 为数组
	 */
	function _output_json($data) {
		$this->load->library('JsonTool');
		$this->output->set_output($this->jsontool->array2json($data));
	}
	
	/*
	 * _view
	 */
	function _view($path,$data=null) {
		if(isset($data))
			$this->load->view($path,$data);
		else 
			$this->load->view($path);
	}
	
	/**
	 * 
	 * @param $id
	 * @param $action
	 * @param $bean_file
	 * @param $data
	 * @param $complete = array('reset'=>'true',callback=>'',redirect=>'',template=>'');
	 * @param $submit_type form提交方式：submit、ajax
	 */
	function _form($id,$action,$bean_file_path,$data=null,$complete=array(),$submit_type=null) {
		include_once(APPPATH.'controllers'.$bean_file_path);
		$complete = is_array($complete)?$complete:array();
		$this->load->library('FormView',array('form_complete'=>$complete));
		return $this->formview->getForm("form_$id",site_url($action),$form_bean,$data,$submit_type);
	}
	
	/**
	 * 公共分页
	 * @param $url
	 * @param $total 总数据条数
	 * @param $size  单页数据条数
	 * @param $index 第几页
	 * @param $is_query_string (true/false)是否有查询参数,如果为true，需要$_GET['index']获取当前页码
	 * @return 分页的html代码
	 */
	function _pages($url,$total,$size,$index,$is_query_string=false){
		$this->load->library('Pagination');
		return $this->pagination->get_pages(site_url($url),$total,$size,$index,$is_query_string);
	}
	
	/*
	 * 递归树
	 */
	function _findChild(&$arr,$id){
		$childs=array();
		foreach ($arr as $k =>$v){
			if($v['parentid']== $id){
				$childs[]=$v;
			}
		}
		return $childs;
	}
	
	function _build_tree($rows,$id=0){
		$childs=$this->_findChild($rows,$id);
		if(empty($childs)){
			return null;
		}
		foreach ($childs as $k => $v){
			$rescurTree= $this->_build_tree($rows,$v['id']);
			if( null != $rescurTree){
				$childs[$k]['childs']=$rescurTree;
			}
		}
		return $childs;
	}
	
	function _sendmail($to_email,$email_subject,$email_body,$from_name='52DNS',$from_email='litong@3jia5.com'){

		$this->load->library('Email');
	
		$this->email->from($from_email, $from_name);
		$this->email->to($to_email); 
		
		$this->email->subject($email_subject);
		$this->email->message($email_body); 

		if ($this->email->send()){
			log_message('debug',$this->email->print_debugger());
			return true;
		}else{
			log_message('error',$this->email->print_debugger());
//			show_error($this->email->print_debugger());
			return false;
		}
	}
}

class Auth_Controller extends MY_Controller{
	
	function __construct(){
		parent::__construct();
		$login = $this->session->userdata('login');
		if(!$login)
			redirect('login?redirectURL='.urlencode(current_url()));
	}
}

class Manager_Controller extends MY_Controller{
	var $login = array();
	function __construct(){
		parent::__construct();
		$this->login = $this->session->userdata('managerLogin');
		if(!$this->login)
			redirect('login/index/manager?redirectURL='.urlencode(current_url()));
		else if($this->login['username'] != 'admin'){
			if(strpos($this->uri->uri_string,'manager/index')>-1)
				return;
			$permissions = $this->session->userdata('permissions');
//			print_r($permissions);
			if(empty($permissions))
				redirect('manager/index/error');
			foreach( $permissions as $p ) {
				if(strpos($this->uri->uri_string,$p['url']) > -1)
					return;
			}
			redirect('manager/index/error');
		}
	}
}
?>